import 'package:flutter/material.dart';
import 'package:landlore/customWidget/recordBody.dart';
import 'package:landlore/model/orderVO.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:landlore/model/roomCheckInOut.dart';
import 'package:landlore/screens/outputBillCheckInOutDisplayScreen.dart';

import '../dbUtil/databaseHelper.dart';

/***
 * 账本详情页面
 */
class RecordDetailFullScreen extends StatefulWidget {
  final OrderVO orderOV;

  const RecordDetailFullScreen({super.key, required this.orderOV});

  @override
  State<RecordDetailFullScreen> createState() => _RecordDetailFullScreenState();
}

class _RecordDetailFullScreenState extends State<RecordDetailFullScreen> {
  List <OrderVO>roomList = [];
  List<RoomCheckInOut> checkInOutList = [];

  void initState() {
    // TODO: implement initState
    _getRoomList();
    _getCheckInOutList();
    super.initState();
  }

  _getRoomList() async {
    DatabaseHelper dbHelper = DatabaseHelper.instance;
    List<OrderVO> resultData =
        await dbHelper.getOrderByRoomId(widget.orderOV.roomId!);
    setState(() {
      roomList = resultData;
    });
  }

  _getCheckInOutList() async {
    DatabaseHelper dbHelper = DatabaseHelper.instance;
    List<RoomCheckInOut> resultData =
        await dbHelper.getCheckInOutDataByRoomId(widget.orderOV.roomId!);
    setState(() {
      checkInOutList = resultData;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      ///设置文字大小不随系统设置改变
      data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: const Color(0xffe9c46a),
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back_ios,
              color: Colors.black,
              size: 35.sp,
            ),
            onPressed: () => Navigator.pop(context),
          ),
        ),
        body: roomList.isEmpty
            ? Text(
                '暂无可以信息',
                style: TextStyle(
                    fontFamily: '欢字卡通手书', fontSize: 50.sp, color: Colors.black),
              )
            : ListView.builder(
                itemCount: roomList.length + checkInOutList.length,
                itemBuilder: (context, index) {
                  if (index < checkInOutList.length) {
                    return GestureDetector(
                      onTap: (){
                        checkInOutList[index].roomNum=roomList[0].roomNum;
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => OutputBillCheckInOutDisplayScreen(roomCheckInOutVO: checkInOutList[index])));
                      },
                      child: Container(
                        color: Colors.grey,
                        padding:
                            EdgeInsets.only(left: 10.w, right: 10.w, top: 10.h),
                        child: Container(
                          padding: EdgeInsets.all(3.h),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(20.r)),
                            gradient: LinearGradient(
                              colors: [
                                checkInOutList[index].state == 1
                                    ? Colors.lightGreenAccent
                                    : Colors.redAccent,
                                checkInOutList[index].state == 1
                                    ? Colors.lightGreen
                                    : Colors.red,
                              ],
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: checkInOutList[index].state == 1
                                    ? Colors.lightGreen.withOpacity(0.5)
                                    : Colors.red.withOpacity(0.5),
                                blurRadius: 10,
                                offset: Offset(0, 5),
                              ),
                            ],
                          ),
                          child: ListTile(
                            leading: Text(
                              checkInOutList[index].state == 1 ? '入住' : '退房',
                              style: TextStyle(
                                color: Colors.black54,
                                fontWeight: FontWeight.bold,
                                fontSize: 30.sp,
                              ),
                            ),
                            title: Text(
                              '${checkInOutList[index].createDate!.year}年${checkInOutList[index].createDate!.month}月${checkInOutList[index].createDate!.day}日',
                              style: TextStyle(
                                color: Colors.black54,
                                fontSize: 22.sp,
                              ),
                            ),
                            subtitle: Text(
                              checkInOutList[index].state == 1 ?
                              '${checkInOutList[index].contact!}  押:${checkInOutList[index].deposit!.toStringAsFixed(0)}'
                              :checkInOutList[index].contact!,
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 25.sp,
                              ),
                            ),
                            trailing: ElevatedButton(
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: Colors.white70, // 按钮的背景颜色
                                ),
                                onPressed: () {
                                  // 处理删除操作
                                  showDialog(context: context, builder: (BuildContext context){
                                    return AlertDialog(
                                      title: const Text('注意'),
                                      content:const Text('确定要删除此条记录吗'),
                                      actions: [TextButton(onPressed: (){
                                        //删除记录
                                        DatabaseHelper.instance.deleteData(TableName.ROOM_CHECK_IN_OUT, primaryKey: 'id', primaryKeyValue: checkInOutList[index].id!);
                                        //刷新数据
                                        _getCheckInOutList();
                                        //关闭弹框
                                        Navigator.pop(context);
                                      },
                                          child: const Text('确认'))],
                                    );
                                  });
                                },
                                child: Text(
                                  '删除',
                                  style: TextStyle(
                                    color: Colors.black54,
                                    fontSize: 16.sp,
                                  ),
                                )),
                          ),
                        ),
                      ),
                    );
                  } else {
                    int tempIndex = index - checkInOutList.length;
                    print(tempIndex);
                    return Container(
                      color: Colors.grey,
                      child: Column(
                        children: [
                          RecordBody(
                            orderVO: roomList[tempIndex],
                            forceChangeState: false,
                          ),
                          roomList.length - 1 == index
                              ? SizedBox(
                                  height: 50.h,
                                )
                              : Container(
                                  width: 120.w,
                                  height: 120.h,
                                  decoration: BoxDecoration(
                                      color: const Color(0xfff4a261),
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(100.r))),
                                  // padding: EdgeInsets.all(20),
                                  child: Icon(
                                    Icons.unfold_more_double,
                                    color: Colors.black,
                                    size: 100.sp,
                                  )),
                        ],
                      ),
                    );
                  }
                }),
      ),
    );
  }

  getSum(OrderVO orderOV) {
    return (orderOV.rent! +
            (orderOV.otherCharge == null ? 0 : orderOV.otherCharge!) +
            (orderOV.electricityCharge == null
                ? 0
                : orderOV.electricityCharge!) +
            (orderOV.waterCharge == null ? 0 : orderOV.waterCharge!))
        .toStringAsFixed(1);
  }
}
